import Vue from 'vue'
import { Toast } from 'vant';
Vue.use(Toast);
// 引入vuex方法的名字，在下边加[]是因为方法名不能为一个字符串，就相当于对象的[]语法
import {ADD_GOODS,GIVE_GOODS,ADD_NUM,REDUCE_GOODS,REMOVE_GOODS,REMOVE_XZGOODS,USER,AUTOLOGIN} from "./mutations_type"
import state from "./state"
import {setStorage,getStorage,removeStorage} from "@/plugins/localStorage"
import {reqCartApi} from "@/api"
export default {
	//添加商品
	[ADD_GOODS](state,item){
		let {shopcart} = state
		// id:goods_id,name:goods_name,price:goods_price,small_image
		let {goods_id:id,goods_name:name,small_image,goods_price:price,checked:isChecked,num} = item
		let shujv = {id,name,small_image,price,num,isChecked}
		//进行判断是否登录
		//通过id判断shopcart是否已有此商品
		if(shopcart[id]){
			//如果有此商品num++
			shopcart[id].num++
		}else{
			// 将数据加入shopcart
			shopcart[id] = shujv
		}
		//1.发送ajax给服务端，若返回true
		//2.就更新vuex数据
		
		//3.同步更新历史记录
		setStorage("shopcart",shopcart)
		state.shopcart = {...shopcart}
		//添加成功提示
		Toast.success({
			duration:500,
			message:'添加成功'
		});
	},
	//点击加号
	[ADD_NUM](state,item){
		let {shopcart} = state
		// id:goods_id,name:goods_name,price:goods_price,small_image
		let {goods_id:id,goods_name:name,small_image,goods_price:price,checked:isChecked,num} = item
		let shujv = {id,name,small_image,price,num,isChecked}
		//进行判断是否登录
		//通过id判断shopcart是否已有此商品
		if(shopcart[id]){
			//如果有此商品num++
			shopcart[id].num = num
		}else{
			Toast.success({
				duration:500,
				message:'添加失败'
			});
			return
		}
		//1.发送ajax给服务端，若返回true
		//2.就更新vuex数据
		
		//3.同步更新历史记录
		setStorage("shopcart",shopcart)
		state.shopcart = {...shopcart}
		//添加成功提示
		Toast.success({
			duration:500,
			message:'添加成功'
		});
	},
	//递减商品
	[REDUCE_GOODS](state,item){
		let {shopcart} = state
		let {id,num} = item
		if(num<=1)return
		shopcart[id].num = num
		setStorage("shopcart",shopcart)
		state.shopcart = {...shopcart}
	},
	//从历史中获取数据，将state实时同步
	[GIVE_GOODS](state){
		let {shopcart} = state
		shopcart = getStorage("shopcart")
		state.shopcart = {...shopcart}
	},
	//删除购物车中某一项商品
	[REMOVE_GOODS](state,id){
		let {shopcart} = state
		removeStorage("shopcart",id)
		shopcart = getStorage("shopcart")
		state.shopcart = {...shopcart}
	},
	//删除购物车中选中的商品
	[REMOVE_XZGOODS](state){
		let {shopcart} = state
		let obj = {}
		let arr = Object.values(shopcart).forEach(item=>{
			if(!item.isChecked){
				obj[item.id] = item
			}
		})
		setStorage("shopcart",obj)
		state.shopcart = {...obj}
	},
	//将数据保存到本地与内存
	[USER](state,users){
		state.isDenglu = users
		setStorage("user",users)
	},
	//自动登录
	[AUTOLOGIN](state){
		let res = getStorage('user')
		if(!res)return
		state.isDenglu = res
		//拿到token值去请求服务端数据
			// setStorage("shopcart",obj)
			// state.shopcart = obj
	}
}





// import Vue from 'vue'
// import { Toast } from 'vant';
// Vue.use(Toast);
// import {ADD_GOODS} from './mutations_type'
// import state from './state'
// export default {
// 	[ADD_GOODS](state,item){
// 		let {shopcart} = state
// 		let {id,name,small_image,price} = item
// 		let arr = {id,name,small_image,price,num:1,isChecked:true}
// 		if(shopcart[id]){
// 			shopcart[id].num++
// 		}else{
// 			shopcart[id] = arr
// 		}
// 		state.shopcart = {...shopcart}
// 		Toast.success({
// 			message:'添加成功',
// 			duration:500
// 		})
// 	}
// }